############################################################################
#
# getCMImage - image extractor for cablemodems
# 2010,2011
#  
# Alejandro Alvarez
# ...
# 
# 
# Released under GPL
#
############################################################################

echo "=> Inicializando..."
fallo="0";
./cg-init.exp 1> /dev/null

if [ ! -f part1.fla ]; then 
echo "=> Extrayendo part1..."
rm -f salida.txt
./cg-flash.exp 0 64 1> /dev/null
./parser.sh part1.fla
if [ $? -eq 1 ]; then
	fallo="1"	
fi
else
echo "=> Fichero part1.fla ya existe, skipping"
fi

if [ ! -f part2.fla ]; then
echo "=> Extrayendo part2..."
rm -f salida.txt
./cg-flash.exp 64 128 1> /dev/null
./parser.sh part2.fla
if [ $? -eq 1 ]; then
        fallo="1"
fi
else
echo "=> Fichero part2.fla ya existe, skipping"
fi

if [ ! -f part3.fla ]; then
echo "=> Extrayendo part3..."
rm -f salida.txt
./cg-flash.exp 128 192 1> /dev/null
./parser.sh part3.fla
if [ $? -eq 1 ]; then
        fallo="1"
fi
else
echo "=> Fichero part3.fla ya existe, skipping"
fi

if [ ! -f part4.fla ]; then
echo "=> Extrayendo part4..."
rm -f salida.txt
./cg-flash.exp 192 256 1> /dev/null
./parser.sh part4.fla
if [ $? -eq 1 ]; then
        fallo="1"
fi
else
echo "=> Fichero part4.fla ya existe, skipping"
fi

if [ ! -f part5.fla ]; then
echo "=> Extrayendo part5..."
rm -f salida.txt
./cg-flash.exp 256 320 1> /dev/null
./parser.sh part5.fla
if [ $? -eq 1 ]; then
        fallo="1"
fi
else
echo "=> Fichero part5.fla ya existe, skipping"
fi

if [ $fallo -eq 1 ]; then
  echo "=> Algunos ficheros no se han generado. Vuelve a lanzar el script"
  exit 1
fi

echo "=> Merging files..."
rm -f flashdump.all
cat part1.fla >> flashdump.all
cat part2.fla >> flashdump.all
cat part3.fla >> flashdump.all
cat part4.fla >> flashdump.all
cat part5.fla >> flashdump.all
head -306 flashdump.all > hexflash.txt
echo "=> Creado hexflash.txt"

